<div class="alert-title">更换代理</div>

<table class="daili-info">
    <tr>
        <td class="title" style="width:80px;">当前代理：</td>
        <td class="field curr">
        </td>
        <td style="width:60px;"> </td>
    </tr>
    <tr>
        <td class="title">新代理：</td>
        <td class="field">
            <input type="text" class="am-form-field" name="new" placeholder="请输入新代理账号"/>
        </td>
        <td> <button class="search-user">查找</button> </td>
    </tr>
</table>

<div class="button_line">
    <button class="btn-save admin_button">确定</button>
    <button class="close admin_button">取消</button>
</div>
<style type="text/css">
    .daili-info .title {
        width: 100px !important;
    }
    .daili-info {
        width: 100%;
    }
    .daili-info td {
        padding: 5px;
    }
    .daili-info td.title { text-align: right; }
</style>

<script>
    var u = context.center.user;
    var referee = null;
    
    $(".daili-info .curr").html(u.referee || '无');

    $(".daili-info .search-user").click(function() {
        var c = $(".daili-info input[name='new']").val().trim();
        if (!c) return;
        startLoading();
        CMD.find_user_by_name(c, function(r) {
            stopLoading();
            if (r.list.length <= 0) {
                alert2("此用户不存在！", true);
                return;
            }
            referee = r.list[0];
        });
    })
    $(".button_line .close").click(function() {
        $("#switch_daili").remove();
    });
    $(".button_line .btn-save").click(function() {
        if (referee == null) {
            alert2("新的代理不存在，请先点击查找按钮", true);
            return;
        }

        // 更改字段
        startLoading();
        CMD.user_referee(u.id, referee.mobile, function() {
            stopLoading();
            $("#switch_daili").remove();

            // 刷新列表
            _f();
        });
    });
</script>